<?php
/**
 * 商品类别模型
 *
 *
 *
 * * @好商城 (c) 2015-2018 33HAO Inc. (http://www.33hao.com)
 * @license    http://www.33 hao.c om
 * @link       交流群号：138182377
 * @since      好商城提供技术支持 授权请购买shopnc授权
 */

defined('In33hao') or exit('Access Invalid!');

class brokerage_logModel extends Model
{
    /**
     * 缓存数据
     */
    protected $cachedData;

    /**
     * 缓存数据 原H('goods_class')形式
     */
    protected $gcForCacheModel;

    /**
     * 读取列表
     * @param array $condition
     *
     */
    public function getMemberLogList($condition, $page=null, $order='', $field='*', $limit=''){
        $result = $this->table('brokerage_log')->field($field)->where($condition)->page($page)->order($order)->limit($limit)->select();
        return $result;
    }

    /**
     * 读取单条记录
     * @param array $condition
     *
     */
    public function getMemberLog($condition,$order=''){
        $result = $this->table('brokerage_log')->where($condition)->order($order)->find();
        return $result;
    }

    /**
     * 读取列表
     * @param array $condition
     *
     */
    public function getStoreLogList($condition, $page=null, $order='', $field='*', $limit=''){
        $result = $this->table('store_brokerage_log')->field($field)->where($condition)->page($page)->order($order)->limit($limit)->select();
        return $result;
    }

    /**
     * 读取单条记录
     * @param array $condition
     *
     */
    public function getStoreLog($condition,$order=''){
        $result = $this->table('store_brokerage_log')->where($condition)->order($order)->find();
        return $result;
    }

    /**
     * 读取列表
     * @param array $condition
     *
     */
    public function getAgentLogList($condition, $page=null, $order='', $field='*', $limit=''){
        $result = $this->table('agent_brokerage_log')->field($field)->where($condition)->page($page)->order($order)->limit($limit)->select();
        return $result;
    }

    /**
     * 读取单条记录
     * @param array $condition
     *
     */
    public function getAgentLog($condition,$order=''){
        $result = $this->table('agent_brokerage_log')->where($condition)->order($order)->find();
        return $result;
    }

    /**
     * 读取列表
     * @param array $condition
     *
     */
    public function getSqLogList($condition, $page=null, $order='', $field='*', $limit=''){
        $result = $this->table('sq_brokerage_log')->field($field)->where($condition)->page($page)->order($order)->limit($limit)->select();
        return $result;
    }

    /**
     * 读取单条记录
     * @param array $condition
     *
     */
    public function getSqLog($condition,$order=''){
        $result = $this->table('sq_brokerage_log')->where($condition)->order($order)->find();
        return $result;
    }

    /**
     * 新增会员日志
     * @param array $insert
     * @return boolean
     */
    public function addMemberLog($insert) {
        return $this->table('brokerage_log')->insert($insert);
    }

    /**
     * 新增小店日志
     * @param array $insert
     * @return boolean
     */
    public function addStoreLog($insert) {
        return $this->table('store_brokerage_log')->insert($insert);
    }

    /**
     * 新增代理日志
     * @param array $insert
     * @return boolean
     */
    public function addAgentLog($insert) {
        return $this->table('agent_brokerage_log')->insert($insert);
    }

    /**
     * 新增代理日志
     * @param array $insert
     * @return boolean
     */
    public function addSqLog($insert) {
        return $this->table('sq_brokerage_log')->insert($insert);
    }

    public function getHalfYearMemberLogList($member_id,$size,$page) {
        $time = getLastTimeArea(date('Y'),date('m'),$size,$page);
        $log_list = [];
        $where['member_id'] = $member_id;
        foreach ($time as $v) {
            $where['addtime'] = ['between',$v['timeArea']];
            $log['data'] = $this->getMemberLogList($where);
            $log['time'] = $v['date'];
            $log_list[] = $log;
        }
        $where['addtime'] = ['lt',strtotime($time[$size-1]['startMonth']) - 1];
        $result = $this->getMemberLog($where);
        $res['hasmore'] = !empty($result) ? true : false;
        $res['data'] = $log_list;
        return $res;
    }

    public function getHalfYearStoreLogList($store_id,$size,$page) {
        $time = getLastTimeArea(date('Y'),date('m'),$size,$page);
        $log_list = [];
        $where['store_id'] = $store_id;
        foreach ($time as $v) {
            $where['addtime'] = ['between',$v['timeArea']];
            $log['data'] = $this->getStoreLogList($where);
            $log['time'] = $v['date'];
            $log_list[] = $log;
        }
        $where['addtime'] = ['lt',strtotime($time[$size-1]['startMonth']) - 1];
        $result = $this->getStoreLog($where);
        $res['hasmore'] = !empty($result) ? true : false;
        $res['data'] = $log_list;
        return $res;
    }

    public function getHalfYearAgentLogList($member_id,$size,$page) {
        $time = getLastTimeArea(date('Y'),date('m'),$size,$page);
        $log_list = [];
        $where['member_id'] = $member_id;
        foreach ($time as $v) {
            $where['addtime'] = ['between',$v['timeArea']];
            $log['data'] = $this->getAgentLogList($where);
            $log['time'] = $v['date'];
            $log_list[] = $log;
        }
        $where['addtime'] = ['lt',strtotime($time[$size-1]['startMonth']) - 1];
        $result = $this->getAgentLog($where);
        $res['hasmore'] = !empty($result) ? true : false;
        $res['data'] = $log_list;
        return $res;
    }

    public function getHalfYearSqLogList($store_id,$size,$page) {
        $time = getLastTimeArea(date('Y'),date('m'),$size,$page);
        $log_list = [];
        $where['store_id'] = $store_id;
        foreach ($time as $v) {
            $where['addtime'] = ['between',$v['timeArea']];
            $log['data'] = $this->getSqLogList($where);
            $log['time'] = $v['date'];
            $log_list[] = $log;
        }
        $where['addtime'] = ['lt',strtotime($time[$size-1]['startMonth']) - 1];
        $result = $this->getSqLog($where);
        $res['hasmore'] = !empty($result) ? true : false;
        $res['data'] = $log_list;
        return $res;
    }

}
